//
//  CYPublicParameters.m
//  cyjf
//
//  Created by tauCross on 2017/5/10.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYPublicParameters.h"

#import <SAMKeychain.h>
#import <JPUSHService.h>



#define SET_PUBLIC_PARAMETERS(macro_key, macro_value) parameters[macro_key] = macro_value != nil ? macro_value : @"";



@interface CYPublicParameters ()

@property(nonatomic, strong)NSString *jpush_alias;
@property(nonatomic, assign)BOOL jpush_alias_setting;

@end


@implementation CYPublicParameters

+ (instancetype)standardPublicParameters
{
    static CYPublicParameters *publicParameters = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        publicParameters = [[CYPublicParameters alloc] init];
    });
    return publicParameters;
}

- (instancetype)init
{
    self = [super init];
    if(self)
    {
        self.jpush_alias = @"";
        self.app_type = CY_APP_TYPE;
        self.channel = CY_APP_TYPE;
        self.device_id = @"";
        self.device_serial_id = [self uuid];
        self.device_type = @"1";
        self.jpush_type = @"";
        self.phone_system_version = [UIDevice currentDevice].systemVersion;
        self.super_user_id = @"";
        self.version_code = CYAppBuild;
        self.version_name = CYAppVersion;
        self.app_store_id = CY_APP_STORE_ID;
        self.timestamp = @0;
    }
    return self;
}

- (NSString *)token
{
    return CYStandardAuthManager.currentAuth.token ? : @"";
}

- (NSMutableDictionary *)dictionary
{
    [self checkAlias];
    self.timestamp = @((long long)([[NSDate date] timeIntervalSince1970] * 1000));
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    SET_PUBLIC_PARAMETERS(@"app_type", self.app_type)
    SET_PUBLIC_PARAMETERS(@"channel", self.channel)
    SET_PUBLIC_PARAMETERS(@"device_id", self.device_id)
    SET_PUBLIC_PARAMETERS(@"device_serial_id", self.device_serial_id)
    SET_PUBLIC_PARAMETERS(@"device_type", self.device_type)
    SET_PUBLIC_PARAMETERS(@"jpush_type", self.jpush_type)
    SET_PUBLIC_PARAMETERS(@"phone_system_version", self.phone_system_version)
    SET_PUBLIC_PARAMETERS(@"registration_id", self.registration_id)
    SET_PUBLIC_PARAMETERS(@"super_user_id", self.super_user_id)
    SET_PUBLIC_PARAMETERS(@"version_code", self.version_code)
    SET_PUBLIC_PARAMETERS(@"version_name", self.version_name)
    SET_PUBLIC_PARAMETERS(@"timestamp", self.timestamp)
    SET_PUBLIC_PARAMETERS(@"app_store_id", self.app_store_id)
    SET_PUBLIC_PARAMETERS(@"token", self.token)
    return parameters;
}

#pragma mark - Private Method
- (NSString *)uuid
{
    NSString *uuid = [SAMKeychain passwordForService:CYBundleIdentifier account:@"device_id"];
    if(uuid == nil || uuid.length == 0)
    {
        uuid = [NSUUID UUID].UUIDString;
        [SAMKeychain setPassword:uuid forService:CYBundleIdentifier account:@"device_id"];
    }
    return uuid;
}

- (NSString *)registration_id
{
    return [JPUSHService registrationID];
}

- (void)checkAlias
{
    if(self.jpush_alias_setting)
    {
        return;
    }
    if(CYStandardAuthManager.currentAuth.uid != nil)
    {
        if(![CYStandardAuthManager.currentAuth.uid.stringValue isEqualToString:self.jpush_alias])
        {
            @weakify(self);
            self.jpush_alias_setting = YES;
            [JPUSHService setAlias:CYStandardAuthManager.currentAuth.uid.stringValue
                        completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                            @strongify(self);
                            self.jpush_alias = iAlias;
                            self.jpush_alias_setting = NO;
                            DLog(@"set jpush alias complete.");
                        }
                               seq:0];
        }
    }
    else
    {
        if(self.jpush_alias.length)
        {
            @weakify(self);
            self.jpush_alias_setting = YES;
            [JPUSHService deleteAlias:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                @strongify(self);
                self.jpush_alias = @"";
                self.jpush_alias_setting = NO;
                DLog(@"delete jpush alias complete.");
            }
                                  seq:0];
        }
    }
}

@end
